package com.bmob.lover.ui.activity

import android.content.Intent
import com.qmuiteam.qmui.arch.QMUIFragment
import com.qmuiteam.qmui.widget.QMUIProgressBar
import com.qmuiteam.qmui.widget.webview.QMUIWebViewContainer
import com.theone.common.constant.BundleConstant
import com.bmob.lover.viewmodel.LOLArticleDetailViewModel
import com.theone.mvvm.core.app.ext.showLoadingPage
import com.theone.mvvm.core.databinding.FragmentWebExploererBinding
import com.theone.mvvm.core.app.ext.showErrorPage
import com.theone.mvvm.core.app.ext.showLoadingPage
import com.theone.mvvm.core.app.ext.showSuccessPage
import com.theone.mvvm.core.base.activity.BaseWebActivity
import com.theone.mvvm.core.base.callback.IWeb
import java.util.ArrayList

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-26 17:15
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */

fun QMUIFragment.startWebActivity(web: IWeb) {
    requireActivity().run {
        startActivity(Intent(this,LOLWebActivity::class.java).apply {
            putExtra(BundleConstant.DATA,web)
        })
    }
}

class LOLWebActivity : BaseWebActivity<LOLArticleDetailViewModel, FragmentWebExploererBinding>() {

    override fun initData() {
        super.initData()
        getViewModel().mDocId = mIWeb.getWebUrl()
        getViewModel().requestServer()
    }

    override fun onPageReLoad() {
        showLoadingPage()
        getViewModel().requestServer()
    }

    override fun createObserver() {
        getViewModel().getResponseLiveData().observe(this) {
            mWebView.loadData(it.toHtmlData(), "text/html", "utf-8")
            showSuccessPage()
        }
        getViewModel().getErrorLiveData().observe(this) {
            showErrorPage(it)
        }
    }

    override fun startPhotoWatchActivity(itemData: ArrayList<String>, position: Int) {
        itemData.iterator().run {
            while (hasNext()){
                val next = next()
                if(next.contains("apps.game.qq.com") || next.contains("gpcd.gtimg.cn")){
                    remove()
                }
            }
        }
        super.startPhotoWatchActivity(itemData, position)
    }

    private fun String.toHtmlData(): String {
        return "<html> \n" +
                "<head> \n" +
                "<style type=\"text/css\"> \n" +
                "body {font-size:16px;padding:8px;}\n" +
                "img {width:100%;height:auto;}\n" +
                "</style> \n" +
                "</head> \n" +
                "<body>$this</body>" +
                "</html>"
    }

    override fun getProgressBar(): QMUIProgressBar = getDataBinding().progressbar

    override fun getWebContainer(): QMUIWebViewContainer = getDataBinding().mWebContainer

}